﻿Imports BUS
Imports DTO
Imports Windows.Forms
Public Class USChiTietban

    Public _maBanUS As Integer
    Public _maHD As Integer = 0
    Public banDTO As New BanDTO()
    Public banBUS As New BanBUS()
    Public hdBUS As New HoaDonBUS()
    Public ctBUS As New ChiTietBUS()
    Public nvBUS As New NhanVienBUS()
    Public tdBUS As New ThucDonBUS()

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub


    Private Sub USChiTietban_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CapNhatBan()        
    End Sub

    Public Function LayMaBan() As Integer
        Dim _maBan As String
        _maBan = Me.Name.Remove(0, 12)
        Return Integer.Parse(_maBan)
    End Function


    Private Sub ReflectionImageTinhTrangBan_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReflectionImageTinhTrangBan.DoubleClick
        If (banDTO.TinhTrang = 0) Then
            Dim answer As Integer = MessageBox.Show("Cập nhật lại bàn sang trạng thái dùng", "Bạn có muốn?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If (answer = 6) Then
                Dim _banDTO As New BanDTO(_maBanUS, Nothing, Nothing)
                _maHD = banBUS.CapNhatTrangThaiBanDangSuDungBUS(_banDTO)                
            End If
        Else
            Dim answer As Integer = MessageBox.Show("Thanh toán hoá đơn bàn này?", "Bạn có muốn?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If (answer = 6) Then
                Dim _frmThanhToanHoaDon As New FormThanhToanHD()
                _frmThanhToanHoaDon._maHD = _maHD
                _frmThanhToanHoaDon.ShowDialog()

                If (_frmThanhToanHoaDon._flag = 1) Then
                    Dim _hdDTO As New HoaDonDTO(_maHD, _maBanUS, Nothing, Nothing, Nothing)
                    hdBUS.ThanhToanTienHoaDonBUS(_hdDTO)
                End If
                
            End If
        End If

        CapNhatBan()
        

    End Sub

    Public Sub CapNhatBan()
        _maBanUS = LayMaBan()
        banDTO = banBUS.LayThongTinBanBUS(_maBanUS)
        lbChiTietBan.Text = "Mã bàn: " & _maBanUS & vbNewLine
        Dim _tdDTO As ThucDonDTO = tdBUS.LayThongTinThucDonBUS(banDTO.Loai)
        lbChiTietBan.Text &= "Loại bàn: " & _tdDTO.Ten & vbNewLine
        If (banDTO.TinhTrang = 1) Then

            Dim _hdDTO As New HoaDonDTO(Nothing, _maBanUS, Nothing, Nothing, Nothing)
            _maHD = hdBUS.LayMaHDBanDangSuDungBUS(_hdDTO)
            _hdDTO = hdBUS.LayThongTinHDBUS(_maHD)
            Dim _nvDTO As New DTONhanVien()
            _nvDTO = nvBUS.LayThongTinNhanVienBUS(_hdDTO.IMaNV)
            Dim TongTien As Double = hdBUS.TongTienHDDAO(_maHD)

            Me.ReflectionImageTinhTrangBan.Image = Global._0966005_0966052_QuanLyBi_a.My.Resources.Resources.tao_ban_bia_flash_dadung
            lbChiTietBan.Text = lbChiTietBan.Text & "Đang sử dụng" & vbNewLine
            lbChiTietBan.Text = lbChiTietBan.Text & "NV trực: " & _nvDTO._sTen & vbNewLine            
        Else
            Me.ReflectionImageTinhTrangBan.Image = Global._0966005_0966052_QuanLyBi_a.My.Resources.Resources.tao_ban_bia_flash
        End If
        TinhTrangBan(banDTO.TinhTrang)
    End Sub

    Private Sub btnNhanVienTruc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhanVienTruc.Click
        Dim _formChonNV As New FormChonNhanVien()
        _formChonNV.ShowDialog()
        If (_formChonNV._flag = 1) Then
            Dim _hdDTO As New HoaDonDTO(_maHD, _maBanUS, Nothing, Nothing, _formChonNV._maNV)
            hdBUS.CapNhatNhanVienTrucVaoHoaDonDAO(_hdDTO)
        End If
        CapNhatBan()
    End Sub

    Public Sub TinhTrangBan(ByVal _tinhTrang As Integer)
        If (_tinhTrang = 0) Then
            btnCTThucDon.Enabled = False
            btnNhanVienTruc.Enabled = False
        Else
            btnCTThucDon.Enabled = True
            btnNhanVienTruc.Enabled = True
        End If
    End Sub

    Private Sub btnCTThucDon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCTThucDon.Click
        Dim _formThucDon As New FormChonThucDon()
        _formThucDon.ShowDialog()
        If (_formThucDon._flag = 1) Then
            Dim _chiTietDTO As New ChiTietDTO(Nothing, _maHD, _formThucDon._maThucDon, _formThucDon._soLuong, Nothing, Nothing)
            ctBUS.ThemCTHDBUS(_chiTietDTO)
        End If
        CapNhatBan()
    End Sub


End Class
